package com.rundatop.wx.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.rundatop.dao.mapper.StoreMapper;
import com.rundatop.dao.model.Store;
import com.rundatop.dao.model.User;
import com.rundatop.wx.dto.Letter;
import com.rundatop.wx.dto.Page;
import com.rundatop.wx.service.ProvincesService;
import com.rundatop.wx.service.StoreService;
import com.rundatop.wx.util.WxUtil;

import tk.mybatis.mapper.entity.Example;

/**
* 通用 Mapper 代码生成器
*
* @author mapper-generator
*/
@Service
public class StoreServiceImpl extends BaseService<Store> implements StoreService{
	
	@Resource
    private StoreMapper storeMapper;
	@Resource
    private ProvincesService provincesService;

    @Override
    public int add(Store entity, User loginUser) {

        //if (isBlank(entity.getCode())) {
        //    throw new BusinessException("代码不能为空");
        //}

       return storeMapper.insertSelective(entity);
    }

    @Override
    public int edit(Store entity, User loginUser) {

        //if (isBlank(entity.getId())) {
        //    throw new BusinessException("Id不能为空");
        //}

        return storeMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public int deleteById(Serializable id, User loginUser) {
       return storeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Store> getList(Store condition, Page page, User loginUser) {
        Example ex = new Example(Store.class);

        Example.Criteria c = ex.createCriteria();

        //if (isNotBlank(condition.getCode())) {
        //    c.andEqualTo("code", condition.getCode());
        //}

        //if (isNotBlank(condition.getName())) {
        //    c.andLike("name", fullLike(condition.getName()));
        //}

        //ex.orderBy("code").asc();

        if (page != null) {
            PageHelper.startPage(page.getPage(), page.getRows());
        }

        List<Store> list = storeMapper.selectByExample(ex);
        return list;
    }

    @Override
    public Store getById(Serializable id) {
        return storeMapper.selectByPrimaryKey(id);
    }

	@Override
	public List<Store> getStroe(String id, String loc) {
		if (StringUtils.isNotBlank(id)) {
			Example e = new Example(Store.class);
			e.createCriteria().andEqualTo(Store.CITIES, id);
			List<Store> list = storeMapper.selectByExample(e);
			for (Store store : list) {
				Double distance = WxUtil.getDistance(loc, store.getMap());
				store.setDistance(distance.toString());
				store.setDistanceDouble(distance);
			}
			list.sort(new Comparator<Store>() {
	            @Override
	            public int compare(Store o1, Store o2) {
	            	Double d1 = o1.getDistanceDouble();
	            	Double d2 = o2.getDistanceDouble();
	                return d1.compareTo(d2);
	            }
	        });
			return list;
		}
		return null;
	}

	@Override
	public Map<String, Object> getPoi(String loc) {
		Map<String, Object> dbInfo = WxUtil.getPoi(loc);
		return dbInfo;
	}

}




